package util.proxy;

import java.lang.reflect.Proxy;

/**
 * @author sola
 */
public class ObjectProxy {

    private Object target;
    private Class aClass;

    public ObjectProxy(Object target) {
        this.target = target;
    }

    public ObjectProxy(Class aClass) {
        this.aClass = aClass;
    }

    public ObjectProxy() {
    }

    public Object getProxyInstanceByObject(Object object){
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),
                (proxy, method, args) -> method.invoke(object,args));
    }
    public Object getProxyInstanceByObject(){
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(),
                (proxy, method, args) -> method.invoke(target,args));
    }

    public Object getProxyInstance(Class aClass){
        return Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(),
                (proxy, method, args) -> method.invoke(aClass.newInstance(),args));
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(aClass.getClassLoader(), aClass.getInterfaces(),
                (proxy, method, args) -> method.invoke(aClass.newInstance(),args));
    }
    public void show(Class aClass){
        ClassLoader classLoader1=aClass.getClassLoader();
         Class<?>[] aClass1=aClass.getInterfaces();
        ClassLoader classLoader2=aClass.getClass().getClassLoader();
        Class<?>[] aClass2=aClass.getClass().getInterfaces();
        System.out.println("==================================");
    }
    public void show(Object target){
        ClassLoader classLoader2=target.getClass().getClassLoader();
        Class<?>[] aClass2=target.getClass().getInterfaces();
        System.out.println("==================================");
    }
    public static void main(String[] args){

        new ObjectProxy().show(new DellComputer());

        new ObjectProxy().show(DellComputer.class);

        Computer dellComputer = (Computer) new ObjectProxy().getProxyInstanceByObject(new DellComputer());
        dellComputer.openFinish();

        Computer dellComputer2 = (Computer) new ObjectProxy(new DellComputer()).getProxyInstanceByObject();

        dellComputer2.openFinish();

        Computer dellComputer3 = (Computer) new ObjectProxy(DellComputer.class).getProxyInstance();
        dellComputer3.openFinish();

        Computer dellComputer4 = (Computer) new ObjectProxy().getProxyInstance(DellComputer.class);
        dellComputer4.openFinish();

    }

}
